package com.milo.thread;

/**
 * @author: Milogenius
 * @create: 2019-04-25 10:08
 * @description: volatile学习
 **/
public class VolatileFeaturesExample {

    /**
     * 使用volatile声明64位的long型变量
     */
    volatile long vl = 0L;

    /**
     * 单个volatile变量的写
     *
     * @param l
     */
    public void set(long l) {
        vl = l;
    }

    /**
     * 复合（多个）volatile变量的读/写
     */
    public void getAndIncrement() {
        vl++;
    }

    /**
     * 单个volatile变量的读
     *
     * @return
     */
    public long get() {
        return vl;
    }

    public static void main(String[] args) {
        VolatileFeaturesExample example = new VolatileFeaturesExample();
        example.getAndIncrement();
        System.out.println(example.get());
    }
}
